<template>
  <div class="standard-reports">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <h1 class="page-title">标准报表</h1>
        <p class="page-description">预定义的常用业务报表，支持快速生成和导出</p>
      </div>
      <div class="header-actions">
        <el-button type="primary" @click="refreshReports">
          <el-icon><Refresh /></el-icon>
          刷新数据
        </el-button>
      </div>
    </div>

    <!-- 报表分类筛选 -->
    <div class="filter-section">
      <el-card class="filter-card">
        <div class="filter-content">
          <div class="filter-item">
            <label>报表分类：</label>
            <el-select v-model="selectedCategory" placeholder="选择分类" @change="handleCategoryChange">
              <el-option label="全部" value="all" />
              <el-option label="用户分析" value="user" />
              <el-option label="销售分析" value="sales" />
              <el-option label="商品分析" value="product" />
              <el-option label="运营分析" value="operation" />
            </el-select>
          </div>
          <div class="filter-item">
            <label>时间范围：</label>
            <el-date-picker
              v-model="dateRange"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              @change="handleDateChange"
            />
          </div>
        </div>
      </el-card>
    </div>

    <!-- 报表列表 -->
    <div class="reports-grid">
      <el-row :gutter="20">
        <el-col 
          v-for="report in filteredReports" 
          :key="report.id" 
          :xs="24" 
          :sm="12" 
          :md="8" 
          :lg="6"
        >
          <el-card class="report-card" shadow="hover" @click="handleReportClick(report)">
            <div class="report-icon">
              <el-icon :size="32" :color="report.color">
                <component :is="report.icon" />
              </el-icon>
            </div>
            <div class="report-info">
              <h3 class="report-title">{{ report.title }}</h3>
              <p class="report-description">{{ report.description }}</p>
              <div class="report-meta">
                <span class="report-category">{{ report.categoryName }}</span>
                <span class="report-update">{{ report.lastUpdate }}</span>
              </div>
            </div>
            <div class="report-actions">
              <el-button size="small" type="primary" @click.stop="generateReport(report)">
                <el-icon><Document /></el-icon>
                生成
              </el-button>
              <el-button size="small" @click.stop="previewReport(report)">
                <el-icon><View /></el-icon>
                预览
              </el-button>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 报表预览对话框 -->
    <el-dialog
      v-model="previewDialogVisible"
      :title="currentReport?.title"
      width="80%"
      top="5vh"
      class="report-preview-dialog"
    >
      <ReportPreview 
        v-if="previewDialogVisible && currentReport"
        :report="currentReport"
        :date-range="dateRange"
        @export="handleExport"
      />
    </el-dialog>

    <!-- 加载状态 -->
    <div v-loading="loading" class="loading-container" />
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { 
  Refresh, Document, View, User, TrendCharts, 
  ShoppingBag, Setting, DataAnalysis, Monitor 
} from '@element-plus/icons-vue'
import ReportPreview from './components/ReportPreview.vue'

// 响应式数据
const loading = ref(false)
const selectedCategory = ref('all')
const dateRange = ref([])
const previewDialogVisible = ref(false)
const currentReport = ref(null)

// 标准报表列表
const standardReports = ref([
  {
    id: 'user-overview',
    title: '用户概览报表',
    description: '用户总数、活跃用户、新增用户等核心指标',
    category: 'user',
    categoryName: '用户分析',
    icon: 'User',
    color: '#409EFF',
    lastUpdate: '2024-01-15',
    template: 'user-overview'
  },
  {
    id: 'user-behavior',
    title: '用户行为分析报表',
    description: '用户访问路径、页面停留时间、跳出率分析',
    category: 'user',
    categoryName: '用户分析',
    icon: 'Monitor',
    color: '#67C23A',
    lastUpdate: '2024-01-15',
    template: 'user-behavior'
  },
  {
    id: 'sales-summary',
    title: '销售汇总报表',
    description: '销售额、订单量、客单价等销售核心数据',
    category: 'sales',
    categoryName: '销售分析',
    icon: 'TrendCharts',
    color: '#E6A23C',
    lastUpdate: '2024-01-15',
    template: 'sales-summary'
  },
  {
    id: 'product-analysis',
    title: '商品分析报表',
    description: '热销商品、库存分析、商品转化率统计',
    category: 'product',
    categoryName: '商品分析',
    icon: 'ShoppingBag',
    color: '#F56C6C',
    lastUpdate: '2024-01-15',
    template: 'product-analysis'
  },
  {
    id: 'operation-kpi',
    title: '运营KPI报表',
    description: '关键运营指标监控和趋势分析',
    category: 'operation',
    categoryName: '运营分析',
    icon: 'DataAnalysis',
    color: '#909399',
    lastUpdate: '2024-01-15',
    template: 'operation-kpi'
  }
])

// 计算属性
const filteredReports = computed(() => {
  if (selectedCategory.value === 'all') {
    return standardReports.value
  }
  return standardReports.value.filter(report => report.category === selectedCategory.value)
})

// 方法
const refreshReports = async () => {
  loading.value = true
  try {
    // 模拟API调用
    await new Promise(resolve => setTimeout(resolve, 1000))
    ElMessage.success('数据刷新成功')
  } catch (error) {
    ElMessage.error('数据刷新失败')
  } finally {
    loading.value = false
  }
}

const handleCategoryChange = () => {
  // 分类变化处理
}

const handleDateChange = () => {
  // 日期范围变化处理
}

const handleReportClick = (report) => {
  currentReport.value = report
  previewDialogVisible.value = true
}

const generateReport = async (report) => {
  loading.value = true
  try {
    // 模拟报表生成
    await new Promise(resolve => setTimeout(resolve, 2000))
    ElMessage.success(`${report.title} 生成成功`)
  } catch (error) {
    ElMessage.error('报表生成失败')
  } finally {
    loading.value = false
  }
}

const previewReport = (report) => {
  currentReport.value = report
  previewDialogVisible.value = true
}

const handleExport = (format) => {
  ElMessage.success(`正在导出为 ${format} 格式...`)
}

// 生命周期
onMounted(() => {
  // 设置默认日期范围为最近30天
  const endDate = new Date()
  const startDate = new Date()
  startDate.setDate(startDate.getDate() - 30)
  
  dateRange.value = [
    startDate.toISOString().split('T')[0],
    endDate.toISOString().split('T')[0]
  ]
})
</script>

<style scoped>
.standard-reports {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 120px);
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 20px;
  padding: 20px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.header-content h1 {
  margin: 0 0 8px 0;
  font-size: 24px;
  color: #303133;
}

.header-content p {
  margin: 0;
  color: #606266;
  font-size: 14px;
}

.filter-section {
  margin-bottom: 20px;
}

.filter-card {
  border-radius: 8px;
}

.filter-content {
  display: flex;
  gap: 20px;
  align-items: center;
  flex-wrap: wrap;
}

.filter-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.filter-item label {
  font-weight: 500;
  color: #606266;
  white-space: nowrap;
}

.reports-grid {
  margin-bottom: 20px;
}

.report-card {
  height: 280px;
  cursor: pointer;
  transition: all 0.3s ease;
  border-radius: 8px;
}

.report-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.report-icon {
  text-align: center;
  margin-bottom: 16px;
  padding: 20px 0;
}

.report-info {
  text-align: center;
  margin-bottom: 16px;
}

.report-title {
  margin: 0 0 8px 0;
  font-size: 16px;
  font-weight: 600;
  color: #303133;
}

.report-description {
  margin: 0 0 12px 0;
  font-size: 12px;
  color: #909399;
  line-height: 1.4;
}

.report-meta {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  color: #C0C4CC;
}

.report-actions {
  display: flex;
  justify-content: center;
  gap: 8px;
}

.loading-container {
  min-height: 200px;
}

.report-preview-dialog :deep(.el-dialog__body) {
  padding: 20px;
  max-height: 70vh;
  overflow-y: auto;
}
</style>
